// packageAdmin/pages/index/screen/screen.js
import {getDate} from "../../../../utils/Util";
Component({
    options: {
        addGlobalClass: true,
        multipleSlots: true
    },

    /**
     * 组件的属性列表
     */
    properties: {
        show: {
            type: Boolean,
            value: false
        },
        params: {
            type: Object,
            value: {}
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        _isAll: true, //是否显示所有
        _screen: [{
            key: "createTime", //查询字段
            title: "按时间",
            list: [
                {name: "所有", value: ""},
                {name: "今天", value: 0},
                {name: "昨天", value: 0},
                {name: "3天内", value: 0}
            ],
            current: 0
        }, {
            key: "type", //查询字段
            title: "按属性",
            list: [
                {name: "所有", value: ""},
                {name: "寻丢", value: "lost"},
                {name: "还拾", value: "found"}
            ],
            current: 0
        }],
        _region: {
            show: false,
            loading: false,
            value: "",  /*北京市,北京市,东城区*/
            current: ""
        },
        _currentScreen: null, //当前操作对象
        _actionSheet: {
            show: false,
            list: []
        },
        _params: {}
    },

    /*
    * 初始化数据
    * */
    ready: function () {
        let currentTimes = new Date(getDate(new Date().getTime()).split(" ")[0]).getTime();
        this.setData({
            ["_screen[0].list"]: [
                {name: "所有"},
                {name: "今天", value: currentTimes},
                {name: "昨天", value: currentTimes - 24 * 3600 * 1000},
                {name: "3天内", value: currentTimes - 36 * 3600 * 1000}
            ]
        })
    },

    /*
    * 数据监听
    * */
    observers: {
        params: function (val) {
            this.data._params = val
        },
    },

    /**
     * 组件的方法列表
     */
    methods: {
        /*
        * 筛选点击操作
        * */
        bindScreen: function (e) {
            try {
                let index = e.currentTarget.dataset.index;
                let name = e.currentTarget.dataset.name;
                let _screen = this.data._screen;
                if (!name) { //筛选
                    let currentList = _screen[index].list;
                    currentList[_screen[index].current].color = "#57bd6a";
                    this.data._currentScreen = index;
                    this.setData({
                        _actionSheet: {
                            show: !name,
                            list: _screen[index].list
                        }
                    })
                } else if (name === "region") { //区域选择
                    if (!this.data._region.show) {
                        this.setData({"_region.show": true})
                    }
                } else if (name === "all" && !this.data._isAll) { //点击所有  重置数据
                    let _params = this.data._params;
                    this.setData({
                        "_actionSheet.list": this.data._actionSheet.list.map(function (item) {
                            delete item.color
                            return item
                        }),
                        "_region.value": "",
                        "_region.current": "",
                        _screen: _screen.map(function (p) {
                            delete _params[p.key]; //删除查询属性
                            p.current = 0;
                            return p
                        }),
                        _currentScreen: null,
                        _isAll: true
                    })
                    delete _params.region;
                    this.triggerEvent("screen", {name: "reset", result: _params}, {});
                }
            } catch (e) {
                console.error(e)
            }
        },

        /*
        *
        * 区域选择事件监听
        * */
        onScreenRegion: function (e) {
            let params = this.data._params;
            let name = e.detail.name;
            let result = e.detail.result;
            if (name === "loading") { //加载中...
                this.setData({"_region.loading": true})
            } else {
                this.setData({"_region.loading": false})
                if (result.value) {
                    this.setData({
                        _isAll: false,
                        "_region.show": false,
                        "_region.value": result.value,
                        "_region.current": result.current
                    })
                    params.region = result.value;
                    this.triggerEvent("screen", {name: "add", result: params}, {});
                }
            }
        },

        /*
        * actionSheet接收函数
        * */
        onActionSheetClose() {
            this.setData({ "_actionSheet.show": false });
        },
        onActionSheetSelect(event) {
            const that = this;
            let pInx = that.data._currentScreen;
            let cList = that.data._screen[pInx].list;
            let cInx = that.data._screen[pInx].current
            let currentChildInx = cList.findIndex(function (item) {
                return item.name === event.detail.name
            })
            if (cInx !== currentChildInx) {
                let params = that.data._params;
                let key = that.data._screen[pInx].key;
                currentChildInx !== 0 ? params[key] = event.detail.value : delete params[key];
                that.setData({
                    ["_screen[" + pInx + "].current"]: currentChildInx,
                    ["_actionSheet.list[" + currentChildInx + "].color"]: "#57bd6a",
                    ["_actionSheet.list[" + cInx + "].color"]: "",
                    _params: params
                }, function () {
                    that.setData({
                        _isAll: (that.data._screen.findIndex(function (item) {
                            return item.current > 0
                        })) === -1
                    })
                })
                that.triggerEvent("screen", {name: "add", result: params}, {});
            }
        }
    }
})
